package com.note.controller;

import com.alibaba.fastjson.JSON;
import com.note.entity.ExpenseList;
import com.note.entity.NoteContent;
import com.note.entity.UserConsumption;
import com.note.entity.UserNoteContent;
import com.note.service.ExpenseListService;
import com.note.service.UserConsumptionService;
import com.note.service.UserService;
import com.note.servlet.Response;
import com.note.servlet.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by XQ on 2017/6/19.
 */
@RestController
public class ExpListController {
    @Autowired
    ExpenseListService expenseListService;
    @Autowired
    UserService userService;
    @Autowired
    UserConsumptionService userConsumptionService;

    /**
     * 添加一条消费记录
     * @param expenseList
     */
    @RequestMapping(value = "/expenseLists",method = RequestMethod.POST)
    public void addExpenseList(@RequestBody ExpenseList expenseList){

        ExpenseList expenseList1 = new ExpenseList();
        expenseList1.setItemName(expenseList.getItemName());
        expenseList1.setConsumption(expenseList.getConsumption());
        expenseList1.setState(expenseList.getState());
        expenseList1.setTime(expenseList.getTime());
        expenseListService.insert(expenseList1);


        UserConsumption userConsumption = new UserConsumption();
        userConsumption.setUid(userService.findUserIdByUserAccount(expenseList.getUserAccount()).getUid());
        userConsumption.setElid(expenseListService.selectIdByItemName(expenseList.getItemName()).getElid());
        userConsumptionService.addOneRecord(userConsumption);
    }

    /**
     * 得到用户的消费记录
     * @param userAccount
     */
    @RequestMapping(value = "/expenseLists/{userAccount}",method = RequestMethod.GET)
    public List<ExpenseList> getExpenseList(@PathVariable String userAccount){
        return expenseListService.selectByUserAccount(userAccount);
    }
}
